#include "MainFrame/MenuBar/FileMenuList.h"

#include "RIVIAFileIO.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>

#include <QTranslator>
#include <QApplication>

#include "MainFrame/CentralWidget/CentralManager.h"

FileMenuList::FileMenuList(const QString &title, QWidget *parent):
    MenuListBase(title, parent),
    m_pNewImageAction(nullptr),
    m_pOpenImageAction(nullptr),
    m_pSaveImageAction(nullptr),
    m_pExitActoin(nullptr),
    m_pchange2Chinese(nullptr),
    m_pchange2English(nullptr),
    m_pTrans(nullptr)
{
    m_pNewImageAction = addOneAction("NewImage", this);
    m_pOpenImageAction = addOneAction("OpenImage", this);
    m_pSaveImageAction = addOneAction("SaveImage", this);
    this->addSeparator();
    m_pchange2Chinese = addOneAction("Chinese Environment", this);
    m_pchange2English = addOneAction("English Environment", this);
    this->addSeparator();
    m_pExitActoin = addOneAction("Exit", this);

    connect(m_pNewImageAction, SIGNAL(triggered()), this, SLOT(newImageSlot()));
    connect(m_pOpenImageAction, SIGNAL(triggered()), this, SLOT(openImageSlot()));
    connect(m_pSaveImageAction, SIGNAL(triggered()), this, SLOT(saveImageSlot()));
    connect(m_pchange2Chinese, SIGNAL(triggered()), this, SLOT(change2ChineseSlot()));
    connect(m_pchange2English, SIGNAL(triggered()), this, SLOT(change2EnglishSlot()));
    connect(m_pExitActoin, SIGNAL(triggered()), this, SIGNAL(closeMainFrame()));
}

FileMenuList::~FileMenuList()
{

}

void FileMenuList::loadTransFile(const QString &strFilePath)
{
    if (nullptr == m_pTrans)
    {
        m_pTrans = new QTranslator;
    }
    else
    {
        qApp->removeTranslator(m_pTrans);
    }
    bool bOK = m_pTrans->load(strFilePath);
    if(bOK)
    {
        qApp->installTranslator(m_pTrans);
    }
}

void FileMenuList::changeEvent(QEvent *event)
{
    QWidget::changeEvent(event);
    switch (event->type())
    {
    case QEvent::LanguageChange:
        initTrans();
        break;
    default:
        break;
    }
}

void FileMenuList::initTrans()
{
    m_pNewImageAction->setText(tr("NewImage"));
    m_pOpenImageAction->setText(tr("OpenImage"));
    m_pSaveImageAction->setText(tr("SaveImage"));
    m_pExitActoin->setText(tr("Exit"));

    m_pchange2Chinese->setText(tr("Chinese Environment"));
    m_pchange2English->setText(tr("English Environment"));
}

void FileMenuList::newImageSlot()
{

}

void FileMenuList::openImageSlot()
{
    //ImportFile
    QString strFileName = QFileDialog(this).getOpenFileName(nullptr, "ImportImage", "../../../../Resource/Images",
                                                            "JPG File(*.jpg);;PNG File(*.png);;All File(*.*)");
    if(strFileName.isEmpty())
        return;

    bool bImportSuccess = false;
    bImportSuccess = RIVIAFileIO().importFile(strFileName);
    if(bImportSuccess)
    {
        QFileInfo fi;
        fi = QFileInfo(strFileName);
        CentralManager::addImageViewWidget(fi.fileName());
        emit openImageSignal(fi.fileName());
    }
}

void FileMenuList::saveImageSlot()
{
    //ExportFile
    QString strFileName = QFileDialog(this).getSaveFileName(nullptr,
                                                            "ExportImage", "../../../../Resource/Images", "JPG File(*.jpg);;PNG File(*.png);;All File(*.*)");
    if(strFileName.isEmpty())
        return;
    if (QFileInfo(strFileName).suffix().isEmpty())
        strFileName.append(".png");
    bool bExportSuccess = false;
    bExportSuccess = RIVIAFileIO().exportFile(strFileName);
    if(bExportSuccess)
    {
        QMessageBox::information(this,"Export Image","Success!");
    }
}

void FileMenuList::change2ChineseSlot()
{
    QString strFilePath = ":/qm/RIVIA_zh.qm";
    loadTransFile(strFilePath);
}

void FileMenuList::change2EnglishSlot()
{
    QString strFilePath = ":/qm/RIVIA_en.qm";
    loadTransFile(strFilePath);
}
